package org.projectname.web.admin.manager;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.hishenframework.core.data.jdbc.template.DataPaging;
import org.hishenframework.core.web.menu.MenuItem;
import org.projectname.entity.manager.Role;
import org.projectname.entity.manager.RoleResource;
import org.projectname.service.admin.manager.ResourceService;
import org.projectname.service.admin.manager.RoleResourceService;
import org.projectname.service.admin.manager.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(ResourceController.PATH)
@MenuItem(type = "账号管理", label = "资源管理", order = 99)
public class ResourceController {

	public final static String PATH = "/admin/manager/resource";
	public final static String RELOAD = "redirect:" + PATH;
	
	@Resource
	private ResourceService resourceService;
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private RoleResourceService roleResourceService;
	
	@RequestMapping
	public String list(Model model, @ModelAttribute DataPaging<org.projectname.entity.manager.Resource> dataPaging, String resource){
		resourceService.search(dataPaging, resource);
		model.addAttribute("roleResources", roleResourceService.getAll());
		model.addAttribute("roles", roleService.getAll());
		return PATH + "/list";
	}
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable int id, Model model){
		List<Role> roles = roleService.getAll();
		List<RoleResource> roleResources = roleResourceService.getByResourceId(id);
		Iterator<Role> iteratorOfRoles = roles.iterator();
		while (iteratorOfRoles.hasNext()) {
			Role tempRole = iteratorOfRoles.next();
			Iterator<RoleResource> iteratorOfRoleResources = roleResources.iterator();
			while (iteratorOfRoleResources.hasNext()) {
				RoleResource tempRoleResource = iteratorOfRoleResources.next();
				if (tempRoleResource.getRoleId() == tempRole.getId()) {
					tempRole.setUsed(true);
				}
			}
		}
		model.addAttribute("roles", roles);
		model.addAttribute("resource", resourceService.get(id));
		return PATH + "/edit";
	}
	
	@RequestMapping("/create")
	public String create(Model model){
		model.addAttribute("roles", roleService.getAll());
		return PATH + "/create";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@Valid org.projectname.entity.manager.Resource resource,RoleResource entity, BindingResult bindingResult){
		if(!bindingResult.hasErrors()){
			int id = (int) resourceService.save(resource);
			if(entity.getRoleIds() == null){
				return RELOAD;
			}
			Iterator<Integer> iteratorOfRoleIds = entity.getRoleIds()
					.iterator();
			while (iteratorOfRoleIds != null && iteratorOfRoleIds.hasNext()) {
				entity.setResourceId(id);
				entity.setRoleId(iteratorOfRoleIds.next());
				roleResourceService.save(entity);
			}
		}
		return RELOAD;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@Valid org.projectname.entity.manager.Resource resource,RoleResource entity, BindingResult bindingResult){
		if(!bindingResult.hasErrors()){
			resourceService.update(resource);
			roleResourceService.deleteByResourceId(entity.getResourceId());
			if(entity.getRoleIds() == null){
				return RELOAD;
			}
			Iterator<Integer> iteratorOfRoleIds = entity.getRoleIds()
					.iterator();
			while (iteratorOfRoleIds != null && iteratorOfRoleIds.hasNext()) {
				entity.setRoleId(iteratorOfRoleIds.next());
				roleResourceService.save(entity);
			}
		}
		return RELOAD;
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id){
		roleResourceService.deleteByResourceId(id);
		resourceService.delete(id);
		return RELOAD;
	}
}
